<?php
// +----------------------------------------------------------------------
// | 店铺审核
// +----------------------------------------------------------------------
namespace app\common\model;
use think\Model;
use think\Validate;
use base\other\Strings;

class ShopApply extends Model
{
	// ----------------
	// ---模型事件---
	//----------------
	protected static function init()
	{
    	//插入前验证
    	self::event('before_insert', function ($param) {
	    	$validate = new Validate;
			$validate->rule([
				'uid|会员'=>'require|integer',
				'phone|手机号'=>'require|integer|min:11|max:11',
				'contacts|姓名'=>'require',
				'level|申请等级'=>'require|integer',
			]);
	        if(!$validate->check($param)){
	        	throw new \think\Exception($validate->getError());
	        }
        });
        
    	//插入或更新后-发送通知
    	self::event('after_write', function ($data) {
    		$title  = '申请处理通知';
    		switch($data->status ?? 0){
			    case 0 && $data->is_del == 0: //申请成功
			    	model('msg')->send(0,0,$title,"有新的店铺开通申请，请注意查收！");
			        break;
			    case 1: //审核通过
			    	$msg = "恭喜您,您的升级申请已经通过申请,请及时登录后台完善店铺信息！";
			    	model('msg')->send(0,$data['uid'],$title,$msg);
			        break;    
			    case -1://审核不通过
			    	$msg = "您申请成为合伙人的申请已经被驳回，驳回备注：";
			    	model('msg')->send(0,$data['uid'],$title, $msg.($data->status_desc) );
			       break;            
			}
        });
    }
    
	// ---------------------
	// ---字段自动完成---
	//---------------------
	protected $insert = ['no'];
	protected function setNoAttr() {
		return Strings::createOrderNo();
    }
    
    //关联等级表
	public function levelInfo(){
        return $this->hasOne('UserLevel','level','level')->bind([
        	'level_name'=>'name',
        	'role_name',
        ]);
    }
    
     //关联等级表
	public function userVip(){
        return $this->hasOne('user_vip','uid','uid');
    }
    
	
	/**
     * 添加申请
     * @param type $uid
     * @return type
     */
	public function addApply($param)
    {
		$model = new self;
		if( $model->where("uid", $param['uid'] )->where("status",'0')->where("is_del",0)->find() ){
			throw new \think\Exception("已存在申请记录未处理！");
		}
    	$model->allowField(true)->save($param);
    	return $model; 
    }
	
	
	/**
     * 申请通过
     * @param type $uid
     * @return type
     */
	public function approval($id)
    {
		$apply = self::field('id,no,status_desc,update_user,update_time,add_time',true)
			->where('status',0)
			->get($id);
			
		if( !$apply ){
			throw new \think\Exception("找不到该申请记录");
		}
		
		if( $apply->level > 2 && !model('shop')->where('uid',$apply->uid)->find() ){
			$param =  $apply->toArray();
			$param['is_enable'] = 1;
			$param['status'] = 0;
			if( !model('shop')->allowField(true)->save($param) ){
				throw new \think\Exception("异常错误！");
			}
		}
		
		$userVip = $apply->userVip;
		$userVip->level = $apply->level;
		$userVip->save();
		
		//保存
		$apply->status = 1;
		$apply->update_user = request() ->userInfo['user_name'];
		$apply->update_time = date('Y-m-d H:i:s');
		$apply->save();
    }
	
	
	/**
     * 驳回
     * @param type $uid
     * @return type
     */
	public function reject($id,$desc)
    {
		if( !$apply = self::where('status',0)->get($id) ){
			throw new \think\Exception("找不到该申请记录");
		}
		$apply->status = -1;
		$apply->status_desc = $desc;
		$apply->update_user = request() ->userInfo['user_name'];
		$apply->update_time = date('Y-m-d H:i:s');
		$apply->save();
    }
	
}
